package sis.ra.partofspeech;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import sis.ra.utility.Utils;

public class AlliasAnalysis {

	public static HashMap<Integer, String> aliasAnalysis(String contentxml)
	{
		HashMap<String, ArrayList<String>> aliashash=new HashMap<String, ArrayList<String>>();
		HashMap<Integer, String> aliasindex=new HashMap<Integer,String>();
		HashMap<String, String> aliasrealnameindex=new HashMap<String,String>();
//		System.out.println(contentxml);
		Document document = null;
		try {
			document = new SAXBuilder().build(new StringReader(contentxml));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element root = document.getRootElement();
		Namespace inx = root.getNamespace("inxenv");
		Namespace inx2 = Namespace.getNamespace("http://www.inxight.com/ims/response/4.6");

		Element docprocnode = root.getChild("payload",inx).getChild("document-processing",inx2);
		Element aliases= docprocnode.getChild("extract-entities",inx2).getChild("entities",inx2).getChild("alias-info",inx2);
//		System.out.println(aliases.getAttributeValue("count"));
		List children = aliases.getChildren();
		String name="";
//		System.out.println(children.size());
		for (Object alias : children) {
			List<Element> aliasgroup=((Element)alias).getChildren();
			ArrayList <String> aliasset=new ArrayList<String>();
			String aliassetname="";
			for (Element aliass:aliasgroup)
			{
				name=aliass.getAttributeValue("name");
				String offset=aliass.getAttributeValue("offset");
				aliasset.add(name+"\t"+offset);
			}
			if (aliassetname.length()>1) 
			{
				aliashash.put(aliassetname, aliasset);
				for (int m=0;m<aliasset.size();m++)
				{
					name=aliasset.get(m).split("\t")[0];
					if (!aliasrealnameindex.containsKey(name))
						aliasrealnameindex.put(name,aliassetname);
				}
			}else
			{
				for (int m=0;m<aliasset.size();m++)
				{
					name=aliasset.get(m).split("\t")[0];
					if (aliasrealnameindex.containsKey(name))
					{
						String realname=aliasrealnameindex.get(name);
						if (!aliashash.containsKey(realname))
							aliashash.put(realname, aliasset);
						else
						{
							ArrayList<String> oldalias=aliashash.get(realname);
//							System.out.println(realname+" "+oldalias.size());
							aliasset.addAll(oldalias);
							aliashash.remove(realname);
							aliashash.put(realname, aliasset);
							break;
						}
					}
				}
			}
		}
		
		Iterator<String> it=aliashash.keySet().iterator();
		while (it.hasNext())
		{
			name=it.next();
			ArrayList<String> lists=aliashash.get(name);
			for (int i=0;i<lists.size();i++)
			{
//				System.out.println(name+" "+lists.get(i));
				String aliasnam=lists.get(i).split("\t")[0];
				Integer offset=Integer.parseInt(lists.get(i).split("\t")[1]);
				if (!aliasindex.containsKey(offset))
					aliasindex.put(offset, aliasnam+"\t"+name);
			}
		}
		return aliasindex;
	}
	
	public static void main(String[] args) {
		String pathname="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\";
		String content=Utils.readFile(pathname+"SAPAG_wiki.txt");
		String contentxml=Utils.getNE(content);
		HashMap<Integer,String> aliasindex=aliasAnalysis(contentxml);
		
		TreeSet<Integer> sortedSet=new TreeSet<Integer>(aliasindex.keySet());
		Object [] sortedArrayne=sortedSet.toArray();
	
		for (int i=0;i<sortedArrayne.length;i++)
		{
			System.out.println(sortedArrayne[i]+" "+aliasindex.get(sortedArrayne[i]));
		}
		
		//output of inverted file of 
//		Iterator<String> itindex=aliasrealnameindex.keySet().iterator();
//		while (itindex.hasNext())
//		{
//			name=itindex.next();
//			System.out.println(name+" "+aliasrealnameindex.get(name));
//		}
		
	}

}
